//
//  ACSViewController.m
//  HomeIn
//
//  Created by 张涛 on 15/11/8.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "ACSViewController.h"
#import "ACSCollectionViewCell.h"
#import "ACTViewController.h"
#import "ACSModel.h"
@interface ACSViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *acsCollention;
@property(nonatomic,strong)NSMutableArray *allArray;
@property(nonatomic,assign)NSInteger currentPage;
@property(nonatomic,assign)BOOL isUpLoading;
@property(nonatomic,strong)MBProgressHUD *hud;

@end

@implementation ACSViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.minimumInteritemSpacing = 5;
    flowlayout.minimumLineSpacing = 5;
    flowlayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
//    flowlayout.itemSize = CGSizeMake(365* WIDTHSCALE, 230 *HEIGHTSCALE);
    //
    self.acsCollention = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowlayout];
    self.acsCollention.delegate = self;
    self.acsCollention.dataSource = self;
    self.acsCollention.backgroundColor = [UIColor whiteColor];
    [self.acsCollention registerClass:[ACSCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.acsCollention];
    self.allArray = [NSMutableArray array];
    //
    [self refresh];

    
}
-(void)refresh
{
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hud show:YES];
    self.currentPage = 10;
    self.isUpLoading = NO;
    [self addHeadRefresh];
    [self addFoodRefresh];
}
-(void)addHeadRefresh
{
    
    
        __block ACSViewController *VC = self;
        //
        self.acsCollention.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            VC.currentPage = 10;
            VC.isUpLoading = NO;
            //
            [VC AFNWorking:VC.currentPage ];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [VC.acsCollention reloadData];
                self.hud.hidden = YES;
            });
            
        }];
        //马上进入刷新状态
        [self.acsCollention.header beginRefreshing];
        
    }
//
-(void)addFoodRefresh
{
    __block ACSViewController *VC = self;
    self.acsCollention.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        VC.currentPage+= 10;
        VC.isUpLoading = YES;
        [VC AFNWorking:VC.currentPage];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [VC.acsCollention reloadData];
            self.hud.hidden = YES;
        });
        
    }];
}
-(void)AFNWorking:(NSInteger)page
{
   
    //
    NSString *str_url = [NSString stringWithFormat:@"http://api.bohejiaju.com/v1/channels/%ld/items?limit=%ld&offset=0",self.ID,page];
   
    [BlockGetAFNYiBu GetAfnURLWithString:str_url resultBlock:^(id result) {
        NSMutableDictionary *bigDic = (NSMutableDictionary *)result;
        NSMutableDictionary *dic = [bigDic objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"items"];
        
        NSMutableArray *temp = [NSMutableArray array];
        for (NSMutableDictionary *smallDic in array)
        {
            ACSModel *model = [[ACSModel alloc]init];
          
           
            [model setValuesForKeysWithDictionary:smallDic];
            
            [temp addObject:model];
        
            self.allArray = temp;
        }
        [self.acsCollention reloadData];
        [self.acsCollention.header endRefreshing];
        [self.acsCollention.footer endRefreshing];
    } errorBlcok:^(NSError *error)
    {
        
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.allArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ACSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    ACSModel *model = [self.allArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(365* WIDTHSCALE, 230 *HEIGHTSCALE);
}
//cell 点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    ACSModel *model = [self.allArray objectAtIndex:indexPath.row];
    ACTViewController *actVC = [[ACTViewController alloc]init];
    //将 id传递到下一个页面,进行拼接
    actVC.ID = model.id;
    
    [self.navigationController pushViewController:actVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
